#pragma once

#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<pthread.h>
#include<unistd.h>
using namespace std;

class ThreadInfo
{
public:
    pthread_t tid_;
    string name_;
};

static const int defaultnum = 5;

template<class T>
class ThreadPool
{
public:
    void Lock()
    {
        pthread_mutex_lock(&mutex_);
    }
    void Unlock()
    {
        pthread_mutex_unlock(&mutex_);
    }
    void Wakeup()
    {
        pthread_cond_signal(&cond_);
    }
    void ThreadSleep()
    {
        pthread_cond_wait(&cond_, &mutex_);
    }
    bool IsQueueEmpty()
    {
        return tasks_.empty();
    }
    string GetThreadName(pthread_t tid)
    {
        for(const auto &ti : threads_)
        {
            //std::cout << tid << ":" << ti.tid_ << std::endl;
            if(ti.tid_ == tid)
            {
                return ti.name_;
            }
        }
        return "None!!!"; // 这里
    }

public:
    static ThreadPool<T> *GetInstance()
    {
        //  这里使用了两个 if判断语句的技术称为双检锁；好处是，只有判断指针为空的时候才加锁，
        //  避免每次调用 GetInstance的方法都加锁，锁的开销毕竟还是有点大的。
        if(nullptr == tp_)
        {
            pthread_mutex_lock(&lock_);
            if(nullptr == tp_)
            {
                cout << "log: singleton has creadted done first!!!" << endl;
                tp_ = new ThreadPool<T>();
            }
            pthread_mutex_unlock(&lock_);
        }
        return tp_;
    }
    void Start()
    {
        int num = threads_.size();
        for(int i = 0; i < num; i++)
        {
            threads_[i].name_ = "thread-" + to_string(i+1);
            pthread_create(&(threads_[i].tid_), nullptr, HandlerTask, this);
            cout << threads_[i].name_ << " has created" << ", tid is " << threads_[i].tid_ << endl;//我加的测试，数据tid能打印，所以存进去了
        }
    }
    static void *HandlerTask(void *arg)
    {
        ThreadPool<T> *tp = static_cast<ThreadPool<T> *>(arg);
        string name = tp->GetThreadName(pthread_self());
        while(true)
        {
            tp->Lock();
            while(tp->IsQueueEmpty())
            {
                tp->ThreadSleep();
            }
            T t = tp->Pop();
            tp->Unlock();

            t();
            cout << name << " run, " << "result: " << t.GetResult() << endl;//这里打印出来，显示 对应thread!!!
        }
    }
    T Pop()
    {
        T t = tasks_.front();
        tasks_.pop();

        return t;
    }
    void Push(const T &t)
    {
        Lock();
        tasks_.push(t);
        Wakeup();
        Unlock();
    }

private:
    ThreadPool(int num = defaultnum):threads_(num)
    {
        pthread_mutex_init(&mutex_, nullptr);
        pthread_cond_init(&cond_, nullptr);
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&mutex_);
        pthread_cond_destroy(&cond_);
    }
    ThreadPool(const ThreadPool<T> &) = delete;
    const ThreadPool<T> operator=(const ThreadPool<T> &) = delete;

private:
    vector<ThreadInfo> threads_;
    queue<T> tasks_;

    pthread_mutex_t mutex_;
    pthread_cond_t cond_;

    static ThreadPool<T> *tp_;
    static pthread_mutex_t lock_;//创建的时候用
};

template<class T>
ThreadPool<T> *ThreadPool<T>::tp_ = nullptr;

template<class T>
pthread_mutex_t ThreadPool<T>::lock_ = PTHREAD_MUTEX_INITIALIZER;